package com.hmall.trade.config;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * feign拦截器（负责从ThreadLocal中获取用户信息设置到请求头）
 *
 * @Author wd
 * @Date 2025/6/20 16:25
 */
@Configuration
public class UserInfoRequestInterceptorConfig {


    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if(userId!=null) {
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }

}
